const app = getApp();
const amap = require('../../utils/amap-wx.js');
const key = app.globalData.key;
let globalData = getApp().globalData;
var myAmap = new amap.AMapWX({ key: key });
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: '3',
    section: app.globalData.section,
    currentName: '',
    aroundList: [
      {
        name: '交通',
        url:'../../images/icon/icon-jiaotong.png',
        id: '150000',
        iconPath: '../../images/icon/icon-jiaotong1.png',
      },
      {
        name: '教育',
        url: '../../images/icon/icon-jiaoyu.png',
        id: '140000',
        iconPath: '../../images/icon/icon-jiaoyu1.png',

      },
      {
        name: '医疗',
        url: '../../images/icon/icon-yiyuan.png',
        id: '090000',
        iconPath: '../../images/icon/icon-yiyuan1.png',
        
      },
      {
        name: '购物',
        url: '../../images/icon/icon-gouwu.png',
        id: '060000',
        iconPath: '../../images/icon/icon-gouwu1.png',
      },
      {
        name: '公园',
        url: '../../images/icon/icon-park.png',
        id: '110000',
        iconPath: '../../images/icon/icon-park1.png',
      }],
    status: null,
    fang:{},
    isShow: false,
    markers: [],
    markersData: [],
    points: [],
    location: '',
    name: '',
    address: '',
    phone: "123456789",
  },
  //点击每个导航的点击事件
  handleTap: getApp().globalData.handleTap,
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    _this.getData();
  },
  getType(e) {//获取选择的附近关键词，同时更新状态
    var markers = this.data.markers;
    this.setData({ 
      status: e.currentTarget.dataset.type,
      markers: markers.pop(),
      })
    this.getAround(e.currentTarget.dataset.keywords, e.currentTarget.dataset.type,e.currentTarget.dataset.id);

  },
  getAround(keywords, types,ids) {//通过关键词获取附近的点，只取前十个，同时保证十个点在地图中显示
    var _this = this;
    myAmap.getPoiAround({
      iconPath: _this.data.aroundList[ids].iconPath,
      querykeywords: keywords,
      querytypes: types,
      location: _this.data.location,
      radius:5000,
      success(data) {
        if (data.markers) {
          var markers = [], 
              points = [],
              markersData = _this.data.markers;
          for (var value of data.markers) {
            if (value.id > 4) break;
            if (value.id == 0) {
              _this.setData({
                name: value.name,
                address: value.address,
                isShow: true
              })
            }
            markers.push({
              id: value.id,
              latitude: value.latitude,
              longitude: value.longitude,
              title: value.name,
              iconPath: _this.data.aroundList[ids].iconPath,
              width: 21,
              height: 25,
              anchor: { x: .5, y: 1 },
              label: {
                content: value.name,
                color: '#000',
                fontSize: 12,
                borderRadius: 5,
                bgColor: '#fff',
                padding: 6,
                x: 0,
                y: -50,
                textAlign: 'center'
              }
            });
            points.push({
              latitude: value.latitude,
              longitude: value.longitude
            })
          }
          var markersAll = markers.concat(markersData);
          _this.setData({
            markers: markersAll,
            points: points
          })
        }
      },
      fail: function (info) {
        wx.showToast({ title: '失败' })
      }
    })
  },
  //电话咨询
  freeTell: function (e) {
    let item = e.currentTarget.dataset.item;
    wx.makePhoneCall({
      phoneNumber: item,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getlocal() {
    var _this = this;
    myAmap.getRegeo({
      success: function (data) {
        var p_name = _this.data.fang.name;
        var p_lat = _this.data.fang.lat;
        var p_lng = _this.data.fang.lng;
        _this.setData({
          markers: [{
            iconPath: "../../images/icon/icon-dingwei.png",
            latitude: p_lat,
            longitude: p_lng,
            width: 32,
            height: 32,
            label: {
              content: p_name,
              color: '#000',
              fontSize: 12,
              borderRadius: 5,
              bgColor: '#fff',
              padding: 6,
              textAlign: 'center'
            }
          }],
          markersData: [{
            iconPath: "../../images/icon/icon-dingwei.png",
            latitude: p_lat,
            longitude: p_lng,
            width: 32,
            height: 32,
            label: {
              content: p_name,
              color: '#000',
              fontSize: 12,
              borderRadius: 5,
              bgColor: '#fff',
              padding: 6,
              textAlign: 'center'
            }
          }],
        });
      },
      fail: function (info) {
        var p_name = _this.data.fang.name;
        var p_lat = _this.data.fang.lat;
        var p_lng = _this.data.fang.lng;
        _this.setData({
          markers: [{
            iconPath: "../../images/icon/icon-dingwei.png",
            latitude: p_lat,
            longitude: p_lng,
            width: 32,
            height: 32,
            label: {
              content: p_name,
              color: '#000',
              fontSize: 12,
              borderRadius: 5,
              bgColor: '#fff',
              padding: 6,
              textAlign: 'center'
            }
          }],
          markersData: [{
            iconPath: "../../images/icon/icon-dingwei.png",
            latitude: p_lat,
            longitude: p_lng,
            width: 32,
            height: 32,
            label: {
              content: p_name,
              color: '#000',
              fontSize: 12,
              borderRadius: 5,
              bgColor: '#fff',
              padding: 6,
              textAlign: 'center'
            }
          }],
        });
      }
    })
  },
  // 进入页面获取数据
  getData() {
    let data = {
      pid: globalData.pid
    }
    globalData.sendPost('project/project-info', data).then(res => {
      if (res.status == 1) {
        this.setData({
          fang: res.data,
          location: res.data.lat + ',' + res.data.lng
        })
        this.getlocal();
      }
    })
  },
})